home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / forth / pfe-0.000 / pfe-0 / pfe-0.9.13 / src / sysdep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-17  |  5.0 KB  |  201 lines

  1. /*
  2.  * This file is part of the portable Forth environment written in ANSI C.
  3.  * Copyright (C) 1995  Dirk Uwe Zoller
  4.  *
  5.  * This library is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU Library General Public
  7.  * License as published by the Free Software Foundation; either
  8.  * version 2 of the License, or (at your option) any later version.
  9.  *
  10.  * This library is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13.  * See the GNU Library General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU Library General Public
  16.  * License along with this library; if not, write to the Free
  17.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  * This file is version 0.9.13 of 17-July-95
  20.  * Check for the latest version of this package via anonymous ftp at
  21.  *    roxi.rz.fht-mannheim.de:/pub/languages/forth/pfe-VERSION.tar.gz
  22.  * or    sunsite.unc.edu:/pub/languages/forth/pfe-VERSION.tar.gz
  23.  * or    ftp.cygnus.com:/pub/forth/pfe-VERSION.tar.gz
  24.  *
  25.  * Please direct any comments via internet to
  26.  *    duz@roxi.rz.fht-mannheim.de.
  27.  * Thank You.
  28.  */
  29. /*
  30.  * sysdep.c ---    put here any definitions missing in your system
  31.  *        most of this file contributed by Marko Teiste
  32.  * (duz 05Aug93)
  33.  */
  34.  
  35. #include "forth.h"
  36.  
  37. #include <stdlib.h>
  38. #include <stdio.h>
  39. #include <limits.h>
  40.  
  41. #if defined WATCOM
  42. #include <i86.h>
  43. #endif
  44.  
  45. #include "nonansi.h"
  46. #include "missing.h"
  47.  
  48.  
  49. /* ========================================================================= */
  50. #ifndef HAVE_AH_TRIG
  51. /* ========================================================================= */
  52.  
  53. /*
  54.  * Simple acosh(), asinh(), atanh() for those unfortunates who don't
  55.  * have them. These are oversimplified routines (no error or boundry
  56.  * checking). !!! DONT TRUST THESE ROUTINES !!!
  57.  */
  58. #include <math.h>
  59.  
  60. double acosh (double n)
  61.     { return log (n + sqrt (n * n - 1)); }
  62. double asinh (double n)
  63.     { return (n < 0 ? -1.0 : 1.0) * log (fabs (n) + sqrt (n * n + 1)); }
  64. double atanh (double n)
  65.     { return log (1.0 + ((2.0 * n) / (1.0 - n))) * 0.5; }
  66.  
  67. /* ========================================================================= */
  68. #endif
  69. #ifndef HAVE_STRDUP
  70. /* ========================================================================= */
  71.  
  72. #include <stdlib.h>
  73. #include <string.h>
  74.  
  75. char *
  76. strdup (const char *s)
  77. {
  78.   static char failed [] = "malloc failed";
  79.   char *p = (char *)malloc (strlen (s) + 1);
  80.   return p ? strcpy (p, s) : failed;
  81. }
  82.  
  83. /* ========================================================================= */
  84. #endif
  85. #ifndef HAVE_MEMMOVE
  86. /* ========================================================================= */
  87.  
  88. void
  89. memmove (char *d, const char *s, unsigned n)
  90. {
  91.   if (n)
  92.     if (s > d)
  93.       do
  94.     *d++ = *s++;
  95.       while (--n > 0);
  96.     else
  97.       do
  98.     {
  99.       --n;
  100.       d [n] = s [n];
  101.     }
  102.       while (n > 0);
  103. }
  104.  
  105. /* ========================================================================= */
  106. #endif
  107. #ifndef HAVE_RENAME
  108. /* ========================================================================= */
  109.  
  110. int
  111. rename (const char *source, const char *target)
  112. /*
  113.  * Rename file,
  114.  * This is not a foolproof routine, one of those
  115.  * "I'll do it better when I have more time" things. -mte
  116.  */
  117. {
  118.   char save_name [PATH_LENGTH+1]; /* Name of saved file */
  119.  
  120.   if (access(target, 0) == 0)
  121.     {
  122.       sprintf (save_name, "%s~", target);
  123.       if (access (save_name, 0) == 0 ||
  124.       unlink (save_name) == -1 ||
  125.       link (target, save_name) == -1 ||
  126.       unlink (target) == -1)
  127.     return -1;
  128.     }
  129.   if (link (source, target) == -1)
  130.     {
  131.       if (save_name != NULL)
  132.     if (link (save_name, target) == -1)
  133.       return -1;
  134.     else
  135.       unlink (save_name);
  136.       return -1;
  137.     }
  138.   if (unlink (source) == -1)
  139.     return -1;
  140.   if (save_name != NULL)
  141.     unlink (save_name);
  142.   return 0;
  143. }
  144.  
  145. /* ========================================================================= */
  146. #endif
  147. #ifndef eXit
  148. /* ========================================================================= */
  149.  
  150. static atexit_fp atexitfun [10];
  151. static int atexitfuns = 0;
  152.  
  153. int
  154. atexit (atexit_fp fun)
  155. {
  156.   atexitfun [atexitfuns++] = fun;
  157.   return 0;
  158. }
  159.  
  160. void
  161. eXit (int n)
  162. {
  163.   int i;
  164.  
  165.   for (i = atexitfuns; --i >= 0; )
  166.     (*atexitfun [i]) ();
  167.   exit (n);
  168. }
  169.  
  170. /* ========================================================================= */
  171. #endif
  172. /* ========================================================================= */
  173.  
  174.  
  175. void
  176. millisec (int ms)
  177. /*
  178.  * Somehow wait ms milli-seconds.
  179.  * Versions using poll and select according to Stevens'
  180.  * "Advanced Programming in the UNIX Environment" p.705
  181.  */
  182. {
  183. #if defined HAVE_DELAY
  184.   delay (ms);
  185. #elif defined EMX
  186.   _sleep2 (ms);
  187. #elif defined HAVE_USLEEP
  188.   usleep (ms * 1000);
  189. #elif defined HAVE_POLL
  190.   static struct pollfd dummy = { 0, POLLHUP, POLLHUP };
  191.   poll (&dummy, 1, ms);
  192. #elif defined HAVE_SELECT || defined HAVE_SYS_SELECT_H
  193.   struct timeval tval;
  194.   tval.tv_sec = ms / 1000;
  195.   tval.tv_usec = ms % 1000 * 1000;
  196.   select (0, NULL, NULL, NULL, &tval);
  197. #else
  198.   sleep ((ms + 999) / 1000);
  199. #endif
  200. }
  201.